#ifndef TFFMPEGSUPPORTER_H
#define TFFMPEGSUPPORTER_H

#include <QObject>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
#include <libavutil/ffversion.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
}

//播放器
struct TFFCustomPlayer {
    AVFormatContext *format_ctx;
    AVCodecContext *v_codec_ctx;
    AVCodecContext *a_codec_ctx;

    int video_stream_idx;
    int audio_stream_idx;

    double audio_clock;
    double audio_pts;
    double video_clock;
    double video_pts;

    QList<AVPacket*> videopktList;
    QList<AVPacket*> audiopktList;

    /*
     * 音频添加
     * packet : 音频包
     */
    void audioAppend(AVPacket* packet) {
        audiopktList.append(packet);
    }

    /*
     * 视频添加
     * packet : 视频包
     */
    void videoAppend(AVPacket* packet) {
        videopktList.append(packet);
    }

    /*
     * 取出一个视频包
     * 返回值 : 视频包
     */
    AVPacket* popVideoFront() {
        AVPacket *packet = videopktList.front();
        videopktList.pop_front();
        return packet;
    }

    /*
     * 取出一个音频包
     * 返回值 : 音频包
     */
    AVPacket* popAudioFront() {
        AVPacket *packet = audiopktList.front();
        audiopktList.pop_front();
        return packet;
    }
};

class TFFmpegSupporter : public QObject
{
    Q_OBJECT
public:
    explicit TFFmpegSupporter(QObject *parent = nullptr);
    static TFFmpegSupporter *get();

    //获取实际PTS
    static int64_t getPts(AVPacket *packet);
    //延时时间值
    static qint64 getDelayTime(AVRational ntimebase, AVPacket *packet, qint64 startTime);
    //根据时间差延时
    static void delayTime(AVRational ntimebase, AVPacket *packet, qint64 startTime);
    //获取FFMPEG错误描述
    static QString getError(int nerrnum);
    //获取播放器参数
    TFFCustomPlayer &getFFCustomPlayer();

private:
    TFFCustomPlayer m_ffcustomPlayer;

signals:

};

#endif // TFFMPEGSUPPORTER_H
